
import org.junit.Test;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 打印类，主要就是用来把文件头进行控制，可以控制双面，多份 纸张类型
 */
public class WritePdf {
    public static final String FONT = "assets/NotoSansCJKsc-Regular.otf";
    public static String paper="A4";//  控制纸的大小 可选   A4 A3 等
    public static String copies="1";//  控制份数    1-999
    public static String duplex="ON";// 双面打印    ON是双面   OFF是单面
    public static String graysclae="1";//控制黑白打印    1是 黑白   3是  彩色打印
    public static String tail = "\u001B%-12345X@PJL EOJ\n" +
            "\u001B%-12345X";
    static String header1 = "\u001B%-12345X@PJL SET STRINGCODESET=UTF8\n"+
            "@PJL JOB MODE=PRINTER";//这个不能去，只能保持这样，不然会出错，好像也不能加\n
    static String header2 = "@PJL JOB NAME=";
    static String header3 =
            "@PJL COMMENT \"87660z (0.3.1584.23029); Unidrv 0.3.15063.138\"\n" +
                    "@PJL COMMENT \"Username: dcy1; App Filename: %1$s; 12-28-2018\"\n" +
                    "@PJL COMMENT \"NUP = NUP_1\"\n" +
                    "@PJL SET JOBATTR=\"Render Type = Discrete V3\"\n" +
                    "@PJL SET JOBATTR=\"Render Name=HP Color LaserJet Pro M452 PCL 6\"\n" +
                    "@PJL SET JOBATTR=\"Render Version=13.26\"\n" +
                    "@PJL SET JOBATTR=\"JobAcct1=dcy2\"\n" +
                    "@PJL SET JOBATTR=\"JobAcct3=AUTH\"\n" +
                    "@PJL SET PAPER="+paper+"\n" +//这里更改了设置
                    "@PJL SET  COPIES="+copies+"\n" +//设置打印份数
                    "@PJL SET DUPLEX="+duplex+"\n" +//设置双面还是单面打印
                    "@PJL SET PLANESINUSE="+graysclae+"\n" +//设置单色打印
                    "@PJL ENTER LANGUAGE=PDF\n";

    /**
     * 这个是调用打印方法，使用打印之前，需要设置参数，以下是默认的，如果想更改的话，就需要调用静态变量赋值
     * paper="A4";//
     * copies="1";//
     * duplex="ON";/
     * graysclae="1"
     *
     */
    @Test
    public  void pt() {
        String sourcePath="D:\\image\\Saved Pictures\\wallhaven-42v990_1920x1080.png";
        String IpAdress="192.168.18.33";
        String test=sourcePath;
        String dest=sourcePath.substring(0,sourcePath.lastIndexOf("."))+"_dest.png";
        File pdf = new File(dest);
        if (pdf.exists()) {
            pdf.delete();
        }
        write(test,dest);
        printNet(dest,IpAdress);

    }
    public  void write(String path, String path2) {
        try {
            FileInputStream fis = new FileInputStream(path);
            FileOutputStream fos = new FileOutputStream(path2);
            //文件尾信息
            String tail = "\u001B%-12345X@PJL EOJ\n" +
                    "\u001B%-12345X";
            fos.write(header1.getBytes());
            fos.write((header2 + "\"" + path2 + "\"" + "\r\n").getBytes());
            fos.write(String.format(header3, path2, "ro.product.model").getBytes());
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = fis.read(b)) > 0) {
                fos.write(b, 0, len);
            }
            fos.write(tail.getBytes());//添加文件尾
            fos.flush();
            fos.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void printNet(String filePath,String IpAdress){
        File file=new File(filePath);

        FileInputStream fis=null;
        DataOutputStream dos=null;
        try {
            Socket client=new Socket(IpAdress,9100);
            fis= new FileInputStream(file);
            dos= new DataOutputStream(client.getOutputStream());//client.getOutputStream()返回此套接字的输出流
            System.out.println("======== 开始传输文件 ========");
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes, 0, bytes.length)) > 0) {
                dos.write(bytes, 0, length);

            }
            dos.flush();
            System.out.println("======== 文件传输成功 ========");
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("客户端文件传输异常");
        }finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }



}
